<%@ page import="java.util.List" %>
<%@ page import="java.io.File" %>
<%@ page import="org.apache.commons.fileupload.FileItemFactory" %>
<%@ page import="org.apache.commons.fileupload.disk.DiskFileItemFactory" %>
<%@ page import="org.apache.commons.fileupload.servlet.ServletFileUpload" %>
<%@ page import="org.apache.commons.fileupload.FileItem" %><%--
  Created by IntelliJ IDEA.
  User: Administrator
  Date: 2021/12/8 0008
  Time: 14:07
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>图片显示</title>
</head>
<body>
<%
    String upLoadfileName="1.png";//上传的文件名
    String fileIdName="";//表单字段name值
    String filePosition="D:\\picture";//上传文件的地址

    FileItemFactory factory = new DiskFileItemFactory();
    ServletFileUpload upload = new ServletFileUpload(factory);
    //解决上传"文件名"的中文乱码
    upload.setHeaderEncoding("UTF-8");

    try{
        List<FileItem> item=upload.parseRequest(request);
        for(FileItem f:item){
            if(f.isFormField()){
                fileIdName=f.getFieldName();
            }else{
                String fileName=f.getName();
                if(fileName!=null&& !"".equals(fileName)){
                    File file=new File(filePosition,upLoadfileName);
                    out.print("上传成功后的文件名是：" + upLoadfileName);
                    out.print("上传成功后的路径是：" + filePosition);
                    f.write(file);
                }
            }
        }


    }catch (Exception e) {
        e.printStackTrace();
    }
%>
</body>
</html>
